//
//  ChatViewController.swift
//  Chatty
//
//  Created by zuber on 2018/9/25.
//  Copyright © 2018年 zuber. All rights reserved.
//

import UIKit

class ChatViewController: UIViewController {
  
  
  public struct Constants {
    public var updatesAnimationDuration: TimeInterval = 0.33
    
  }
  
  public var constants = Constants()
  public private(set) var collectionView: UICollectionView!
  private lazy var layout: ChatCollectionViewLayout = {
    let layout = ChatCollectionViewLayout()
    layout.delegate = self
    return layout
  }()
  
  var layoutModel = ChatCollectionViewLayoutModel.createModel(0, itemsLayoutData: [])
  
  
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.white
    setup()
  }
  
  deinit {
    print("ChatViewController deinited !")
  }
  
}


// MARK: - size changed

extension ChatViewController {
  
  // 首次进入页面 自动滚动到底部
  override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    let shouldScrollToBottom = self.isScrolledAtBottom()
    coordinator.animate(alongsideTransition: { (_) in
      if shouldScrollToBottom{
        self.scrollToBottom(animated: false)
      }
    }, completion: nil)
  }
  
  func rectAtIndexPath(_ indexPath: IndexPath?) -> CGRect? {
    if let indexPath = indexPath {
      return self.collectionView.collectionViewLayout.layoutAttributesForItem(at: indexPath)?.frame
    }
    return nil
  }
}


// MARK: - setup

extension ChatViewController {
  
  func setup(){
    
  }
  
}






